// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-15

package add

import (
	"context"
	"fmt"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/config"
	"server/app/errs"
	"server/app/http/logics/master/setting/seat"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib"
)

// Logic
// 添加钓位.
type Logic struct {
	seat.Logic

	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	affects     int64
	kind        kind.ArcadeAdminOperation
	kindContent string
	pond        *models.ArcadePond

	request  *Request
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// After
// 后置日志.
func (o *Logic) After(ctx context.Context, _ iris.Context, _ result.Result) {
	if o.affects > 0 {
		_, _ = services.NewArcadeLogsService().Add(ctx,
			o.arcade, o.admin, o.kind, o.kindContent,
		)
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Json(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)

	// 1. 事务处理.
	if err = db.Transaction(ctx,
		o.RunCheck,
		o.RunCreate,
		o.RunStatistic,
	); err != nil {
		return res.WithError(err)
	}

	// 2. 保存完成.
	o.response.Id = o.pond.Id
	o.response.Name = o.pond.Name
	return res.WithList(o.response)
}

// RunCheck
// 查询数据.
func (o *Logic) RunCheck(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		total int64
	)

	// 1. 查询鱼塘.
	if o.pond, has, err = services.NewArcadePondService(sess).Dao.GetById(ctx, o.request.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 无效鱼塘.
	if !has || o.pond.ArcadeId != o.arcade.Id {
		err = errs.ErrArcadeSwiperNotFound
		return
	}

	// 3. 数量统计.
	if total, err = o.CalcTotal(sess, o.pond.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 4. 数量限制
	if (int(total) + o.request.Count) > config.Arcade.GetSeatCount() {
		err = errs.ErrArcadeSeatMaxLimited
	}
	return
}

// RunCreate
// 创建钓位.
func (o *Logic) RunCreate(ctx context.Context, sess *db.Session) (err error) {
	var (
		model   *models.ArcadePondSeat
		name    string
		service = services.NewArcadePondSeatService(sess)
	)

	// 1. 循环创建.
	for i := 0; i < o.request.Count; i++ {
		name = fmt.Sprintf("New %d", i+1)

		if model, err = service.Dao.AddByStruct(ctx, &models.ArcadePondSeat{
			ArcadeId: o.arcade.Id,
			PondId:   o.pond.Id,
			Deleted:  kind.DeletedNo,
			Enabled:  kind.EnabledYes,
			Name:     name,
		}); err != nil {
			err = errs.ErrDatabaseQuery
			break
		}

		if _, err = service.Dao.UpdateFieldsById(ctx, map[string]any{
			"deleted": kind.DeletedNo,
		}, model.Id); err != nil {
			err = errs.ErrDatabaseQuery
			break
		}
	}

	// 2. 创建完成.
	if err == nil {
		o.affects = int64(o.request.Count)
		o.kind = kind.ArcadeAdminOperationSeatCreate
		o.kindContent = o.kind.ToSeatCreate(o.pond.Name, o.request.Count)
	}
	return
}

// RunStatistic
// 重建统计.
func (o *Logic) RunStatistic(_ context.Context, sess *db.Session) (err error) {
	if o.response.SeatTotal, err = o.CalcTotal(sess, o.pond.Id); err == nil {
		if o.response.SeatCount, err = o.CalcEnables(sess, o.pond.Id); err == nil {
			err = o.CalcUpdate(sess, o.pond.Id, o.response.SeatTotal, o.response.SeatCount)
		}
	}
	return
}
